home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / misc / emu / ATUtilities.lha / ATUtilities / hc.c < prev    next >
C/C++ Source or Header  |  2000-09-26  |  17KB  |  645 lines

  1. /* ******************************************************************* */
  2. /* **                                                               ** */
  3. /* **                 ATKeyboard Hilfedateiencompiler               ** */
  4. /* **            Copright (C) 1992-1993 by Thomas Dreibholz         ** */
  5. /* **                      All rights reserved                      ** */
  6. /* **                                                               ** */
  7. /* ******************************************************************* */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <libraries/dos.h>
  12.  
  13. #define MAXSIZE(x) ((1000*5*22)+x)+1000000L
  14.  
  15. #define PAGE_INDEX 1
  16. #define PAGE_TEXT  2
  17.  
  18. ULONG x,y,z;
  19. UWORD i,j,k,zeile,a,b,t;
  20. UBYTE style,fp,bp;
  21. WORD  cmd,page;
  22. UBYTE chr;
  23. ULONG offset;
  24. UWORD length;
  25. UBYTE s[500];
  26. UBYTE byte;
  27. UWORD wort;
  28. ULONG langwort;
  29. BOOL  insert=FALSE;
  30.  
  31. UBYTE             *name;
  32. struct FileHandle *in,*out;
  33. UWORD              GoTo[100];
  34. UWORD              GoGad[100];
  35. UBYTE              GoPic[100];
  36. UWORD              GoToCnt;
  37. UWORD              GoGadCnt;
  38. UBYTE              GoPicCnt;
  39. UWORD              DefaultCnt;
  40. UWORD              Default[32];
  41. UBYTE              Buffer[2048];
  42. ULONG              Offset[1000];
  43. ULONG              MOffset[1000];
  44. ULONG              POffset[1000];
  45. ULONG              TOffset[1000];
  46. ULONG              GOffset[1000];
  47. ULONG              PNOffset[1000];
  48. UWORD              Length[1000];
  49. UBYTE              Flags[1000];
  50. UBYTE              mem[250];
  51. UBYTE             *InBuf,*OutBuf;
  52. ULONG              BufferSize;
  53. ULONG              IPos,OPos,IMax;
  54. BOOL               MainIndex;
  55.  
  56.  
  57. VOID Out(m,len)
  58.  UBYTE *m;
  59.  ULONG  len;
  60. {
  61.  REGISTER ULONG i,j;
  62.  
  63.  for(i=0;i<len;i++)
  64.   {
  65.    OutBuf[OPos]=m[i];
  66.    OPos++;
  67.   }
  68. }
  69.  
  70. VOID WriteStyles()
  71. {
  72.  sprintf(&mem,"\x9b%d;%d;%dm",style,fp,bp);
  73.  i=strlen(&mem);
  74.  Out(&mem,i);
  75.  offset+=i;
  76.  length+=i;
  77. }
  78.  
  79. ULONG Get(m,len)
  80.  UBYTE *m;
  81.  ULONG  len;
  82. {
  83.  REGISTER ULONG i,j,x;
  84.  
  85.  x=0;
  86.  for(i=0;((i<len) && (IPos<=IMax));i++)
  87.   {
  88.    m[i]=InBuf[IPos];
  89.    IPos++; x++;
  90.   }
  91.  return(x);
  92.  
  93.  
  94. VOID Stop(num)
  95.  UWORD num;
  96. {
  97.  
  98.  Close(in);
  99.  Close(out);
  100.  DeleteFile(name);
  101.  FreeMem(InBuf,BufferSize);
  102.  switch(num)
  103.   {
  104.    case 1:
  105.      printf("Zeile %ld: #endpage für nicht vorhandene Seite.\n",zeile);
  106.     break;
  107.    case 2:
  108.      printf("Zeile %ld: #endpage erwartet.\n",zeile);
  109.     break;
  110.    case 3:
  111.      printf("Zeile %ld: #goto/#gpic/#gad steht auf keiner Seite.\n",zeile);
  112.     break;
  113.    case 4:
  114.      printf("Zeile %ld: Seitennummerangabe fehlerhaft.\n",zeile);
  115.     break;
  116.    case 5:
  117.      printf("Zeile %ld: Unbekannter Befehl.\n",zeile);
  118.     break;
  119.    case 6:
  120.      printf("Zeile %ld: #space steht auf keiner Seite.\n",zeile);
  121.     break;
  122.    case 7:
  123.      printf("Zeile %ld: Seitentext ohne Seitenangabe durch #page/#index.\n",zeile);
  124.     break;
  125.    case 8:
  126.      printf("Zeile %ld: Ungültige Bildnummer bei #gpic/picture.\n",zeile);
  127.     break;
  128.    case 20:
  129.      printf("Zeile %ld: #picture darf auf keiner Seite stehen.\n",zeile);
  130.     break;
  131.    case 30:
  132.      printf("Zeile %ld: Bild kann nicht geladen werden!\n",zeile);
  133.     break;
  134.    case 40:
  135.      printf("Zeile %ld: Seitennummerangabe bei #default fehlerhaft.\n",zeile);
  136.     break;
  137.   }
  138.  exit(0);
  139. }
  140.  
  141.  
  142. VOID main(argc,argv)
  143.  LONG argc;
  144.  UBYTE *argv[];
  145. {
  146.  struct FileHandle *fh;
  147.  
  148.  printf("\nATUtilities Online-Hilfe Hilfstextcompiler - Version 2.0  25. 11. 1993\n");
  149.  printf("Copyright (C) 1993 by Thomas Dreibholz. All rights reserved.\n");
  150.  printf("----------------------------------------------------------------------\n");
  151.  if(argc<3)
  152.   {
  153.    printf("Aufruf: %s [Quelltext] [Hilfedatei].\n",argv[0]);
  154.    exit(0);
  155.   }
  156.  if(argc>3)
  157.   {
  158.    if(!(strcmp(argv[3],"+I"))) insert=TRUE;
  159.   }
  160.  
  161.  MainIndex=FALSE;
  162.  name=argv[2];
  163.  in=Open(argv[1],MODE_OLDFILE);
  164.  if(in!=NULL)
  165.   {
  166.    Seek(in,0,OFFSET_END);
  167.    IMax=Seek(in,0,OFFSET_BEGINNING);
  168.    BufferSize=IMax+MAXSIZE(IMax);
  169.    InBuf=AllocMem(BufferSize,MEMF_CLEAR|MEMF_PUBLIC);
  170.    if(InBuf!=NULL)
  171.     {
  172.      OutBuf=(UBYTE *)((ULONG)InBuf+IMax);
  173.      IPos=0; OPos=0;
  174.      out=Open(argv[2],MODE_NEWFILE);
  175.      if(out!=NULL)
  176.       {
  177.        printf("Lese Quelltext ...\n");
  178.        Read(in,InBuf,IMax);
  179.        cmd=-1;
  180.        page=-1;
  181.        zeile=0;
  182.        style=0; fp=33; bp=40;
  183.        Out("HELP_2.0",8);
  184.        offset=8;
  185.        printf("Compiliere Quelltext ...\n");
  186.        j=Get(&mem,1);
  187.        chr=mem[0];
  188.  
  189.        while(j!=0)
  190.         {
  191.          if(chr=='#')
  192.           {
  193.            k=0; j=Get(&mem,1);
  194.            while((mem[k]!=10)&&(k<249)&&(j==1))
  195.             {
  196.              k++; j=Get(&mem[k],1);
  197.             }
  198.            mem[k]=0x00;
  199.            zeile++;
  200.            if(j!=0)
  201.             {
  202.  
  203.              if(!(strncmp(&mem,"rem",3)))
  204.               {
  205.                /* Kommentarzeile */
  206.               }
  207.  
  208.              else if(!(strncmp(&mem,"default",7)))
  209.               {
  210.                for(i=0;i<30;i++) Default[i]=0;
  211.                i=8;
  212.                DefaultCnt=0;
  213.                while(i<strlen(&mem))
  214.                 {
  215.                  if((mem[i]>='0')&&(mem[i]<='9'))
  216.                   {
  217.                    k=atol(&mem[i]);
  218.                    i+=4;
  219.                    if(k>999) Stop(40);
  220.                    Default[DefaultCnt]=k; DefaultCnt++;
  221.                   }
  222.                  else
  223.                   {
  224.                    i+=2;
  225.                    Default[DefaultCnt]=2000; DefaultCnt++;
  226.                   }
  227.                 }
  228.               }
  229.  
  230.              else if(!(strncmp(&mem,"endpage",7)))
  231.               {
  232.                style=0; fp=33; bp=40;
  233.                if(page!=-1)
  234.                 {
  235.                  if(GoToCnt>0)
  236.                   {
  237.                    MOffset[page]=offset;
  238.                    for(i=0;i<GoToCnt;i++)
  239.                     {
  240.                      Out(&GoTo[i],2L); offset+=2;
  241.                     }
  242.                    if(DefaultCnt>0)
  243.                     {
  244.                      wort=2000;
  245.                      Out(&wort,2L); offset+=2;
  246.                     }
  247.                   }
  248.                  if(DefaultCnt>0)
  249.                   {
  250.                    if(MOffset[page]==0) MOffset[page]=offset;
  251.                    for(i=0;i<DefaultCnt;i++)
  252.                     {
  253.                      if(Default[i]!=page)
  254.                       {
  255.                        Out(&Default[i],2L); t=Default[i]; offset+=2;
  256.                       }
  257.                      else
  258.                       {
  259.                        if((i>0)&&(Default[i-1]==2000)&&(Default[i+1]==2000))
  260.                         {
  261.                          OPos-=2; offset-=2;
  262.                         }
  263.                        else if((i==0)&&(i<Default-1)&&(Default[i+1]==2000))
  264.                         {
  265.                          i++;
  266.                         }
  267.                       }
  268.                     }
  269.                    if(t==2000)
  270.                     {
  271.                      OPos-=2; offset-=2;
  272.                     }
  273.                   }
  274.                  if(MOffset[page]!=0)
  275.                   {
  276.                    wort=0xFFFF;
  277.                    Out(&wort,2L); offset+=2;
  278.                   }
  279.                  if(GoGadCnt>0)
  280.                   {
  281.                    GOffset[page]=offset;
  282.                    wort=0xFFFF;
  283.                    for(i=0;i<GoGadCnt;i++)
  284.                     {
  285.                      Out(&GoGad[i],2L); offset+=2;
  286.                     }
  287.                    Out(&wort,2L); offset+=2;
  288.                   }
  289.                  if(GoPicCnt>0)
  290.                   {
  291.                    POffset[page]=offset;
  292.                    for(i=0;i<GoPicCnt;i++)
  293.                     {
  294.                      Out(&GoPic[i],1L); offset++;
  295.                     }
  296.                    byte=0xFF;
  297.                    Out(&byte,1L); offset++;
  298.                   }
  299.                  Length[page]=length;
  300.                  GoToCnt=0;
  301.                  GoPicCnt=0;
  302.                  GoGadCnt=0;
  303.                 }
  304.                else Stop(1);
  305.                cmd=-1; page=-1;
  306.               }
  307.  
  308.              else if(!(strncmp(&mem,"page",4)))
  309.               {
  310.                if(page!=-1)
  311.                 {
  312.                  Stop(2);
  313.                 }
  314.                page=atol(&mem[5]);
  315.                if(page>999) Stop(4);
  316.                if(page==0)
  317.                 {
  318.                  MainIndex=TRUE;
  319.                  printf("Info:  Soll Seite 000 keine Indexseite sein?\n");
  320.                 }
  321.                cmd=1;
  322.                Offset[page]=offset;
  323.                MOffset[page]=0;
  324.                TOffset[page]=0;
  325.                Length[page]=0;
  326.                Flags[page]=PAGE_TEXT;
  327.                a=9; b=0;
  328.                while((mem[a]>25)&&(b<290))
  329.                 {
  330.                  s[b]=mem[a]; a++; b++;
  331.                 }
  332.                s[b]=0x00;
  333.                k=strlen(&s)+1;
  334.                offset+=k;
  335.                Out(&s,k);
  336.                length=0;
  337.               }
  338.  
  339.              else if(!(strncmp(&mem,"picture",7)))
  340.               {
  341.                if(page!=-1)
  342.                 {
  343.                  Stop(20);
  344.                 }
  345.                page=atol(&mem[8]);
  346.                if(page>99) Stop(8);
  347.                PNOffset[page]=offset;
  348.                a=11; b=0;
  349.                while((mem[a]>25)&&(b<290))
  350.                 {
  351.                  s[b]=mem[a]; a++; b++;
  352.                 }
  353.                s[b]=0x00;
  354.                if(insert==FALSE)
  355.                 {
  356.                  byte=0;
  357.                  Out(&byte,1L); offset++;
  358.                  k=strlen(&s)+1;
  359.                  offset+=k;
  360.                  Out(&s,k);
  361.                 }
  362.                else
  363.                 {
  364.                  byte=1;
  365.                  Out(&byte,1L); offset++;
  366.                  printf("Info:  Bild %02ld <%s> wird eingefügt - ",page,&s);
  367.                  fh=Open(&s,MODE_OLDFILE);
  368.                  if(fh!=NULL)
  369.                   {
  370.                    Seek(fh,0,OFFSET_END);
  371.                    z=Seek(fh,0,OFFSET_BEGINNING);
  372.                    Out(&z,4L); offset+=4;
  373.                    x=Read(fh,&Buffer,2048);
  374.                    while(x>0)
  375.                     {
  376.                      Out(&Buffer,x); offset+=x;
  377.                      x=Read(fh,&Buffer,2048);
  378.                     }
  379.                    printf("%ld KBytes.\n",z/1024);
  380.                    Close(fh);
  381.                   }
  382.                  else
  383.                   {
  384.                    Stop(30);
  385.                   }
  386.                 }
  387.                page=-1;
  388.               }
  389.  
  390.              else if(!(strncmp(&mem,"index",5)))
  391.               {
  392.                if(page!=-1)
  393.                 {
  394.                  Stop(2);
  395.                 }
  396.                page=atol(&mem[5]);
  397.                if(page==0) MainIndex=TRUE;
  398.                if(page>999) Stop(4);
  399.                cmd=1;
  400.                MOffset[page]=0;
  401.                TOffset[page]=0;
  402.                Length[page]=0;
  403.                Offset[page]=offset;
  404.                Flags[page]=PAGE_INDEX;
  405.                a=10; b=0;
  406.                while((mem[a]>25)&&(b<290))
  407.                 {
  408.                  s[b]=mem[a]; a++; b++;
  409.                 }
  410.                s[b]=0x00;
  411.                k=strlen(&s)+1;
  412.                offset+=k;
  413.                Out(&s,k);
  414.                length=0;
  415.               }
  416.  
  417.              else if(!(strncmp(&mem,"goto",4)))
  418.               {
  419.                if(page!=-1)
  420.                 {
  421.                  k=atol(&mem[5]);
  422.                  if(k>999) Stop(4);
  423.                  if(k!=page)
  424.                   {
  425.                    GoTo[GoToCnt]=k; GoToCnt++;
  426.                   }
  427.                 }
  428.                else Stop(3);
  429.               }
  430.  
  431.              else if(!(strncmp(&mem,"gad",3)))
  432.               {
  433.                if(page!=-1)
  434.                 {
  435.                  k=atol(&mem[4]);
  436.                  if(k>999) Stop(4);
  437.                  if(k!=page)
  438.                   {
  439.                    GoGad[GoGadCnt]=k; GoGadCnt++;
  440.                   }
  441.                 }
  442.                else Stop(3);
  443.               }
  444.  
  445.              else if(!(strncmp(&mem,"gpic",4)))
  446.               {
  447.                if(page!=-1)
  448.                 {
  449.                  k=atol(&mem[5]);
  450.                  if(k>99) Stop(8);
  451.                  GoPic[GoPicCnt]=k; GoPicCnt++;
  452.                 }
  453.                else Stop(3);
  454.               }
  455.  
  456.              else if(!(strncmp(&mem,"space",5)))
  457.               {
  458.                if(page!=-1)
  459.                 {
  460.                  GoTo[GoToCnt]=2000; GoToCnt++;
  461.                 }
  462.                else Stop(6);
  463.               }
  464.  
  465.              else if(!(strncmp(&mem,"gspace",6)))
  466.               {
  467.                if(page!=-1)
  468.                 {
  469.                  GoGad[GoGadCnt]=2000; GoGadCnt++;
  470.                 }
  471.                else Stop(6);
  472.               }
  473.  
  474.              else if(!(strncmp(&mem,"pspace",6)))
  475.               {
  476.                if(page!=-1)
  477.                 {
  478.                  GoPic[GoPicCnt]=200; GoPicCnt++;
  479.                 }
  480.                else Stop(6);
  481.               }
  482.  
  483.              else if(!(strncmp(&mem,"endfile",7)))
  484.               {
  485.                goto ende;
  486.               }
  487.  
  488.              else
  489.               {
  490.                Stop(5);
  491.               }
  492.             }
  493.           }
  494.          else
  495.           {
  496.            zeile++;
  497.            /* Seitentext */
  498.            if(cmd!=-1)
  499.             {
  500.              if(TOffset[page]==0) TOffset[page]=offset;
  501.              if(chr==10)
  502.               {
  503.                mem[0]=0;
  504.                Out(&mem,1); offset++; length++;
  505.               }
  506.              else
  507.               {
  508.                if(Flags[page]==PAGE_TEXT) WriteStyles();
  509.                IPos--;
  510.                j=Get(&mem,1);
  511.                while((j==1)&&(mem[0]!=10))
  512.                 {
  513.                  if((mem[0]=='`')&&(Flags[page]==PAGE_TEXT))
  514.                   {
  515.                    j=Get(&mem[1],1);
  516.                    if((j==1)&&(mem[1]!='`'))
  517.                     {
  518.                      switch(toupper(mem[1]))
  519.                       {
  520.                        case 'F':
  521.                          style=1;
  522.                         break;
  523.                        case 'K':
  524.                          style=3;
  525.                         break;
  526.                        case 'U':
  527.                          style=4;
  528.                         break;
  529.                        case 'I':
  530.                          style=7;
  531.                         break;
  532.                        case 'N':
  533.                          style=0;
  534.                         break;
  535.                        case 'R':
  536.                          style=0; fp=33; bp=40;
  537.                         break;
  538.                        default:
  539.                          if((mem[1]>='0')&&(mem[1]<='8'))
  540.                           {
  541.                            j=Get(&mem[2],1);
  542.                            if(j==1)
  543.                             {
  544.                              if((mem[2]>='0')&&(mem[2]<='8'))
  545.                               {
  546.                                fp=(mem[1]-48)+30;
  547.                                bp=(mem[2]-48)+40;
  548.                               }
  549.                             }
  550.                           }
  551.                         break;
  552.                       }
  553.                      WriteStyles();
  554.                     }
  555.                    else
  556.                     {
  557.                      if(j==1)
  558.                       {
  559.                        mem[0]='`';
  560.                        Out(&mem,1); offset++; length++;
  561.                       }
  562.                     }
  563.                   }
  564.                  else
  565.                   {
  566.                    Out(&mem,1); offset++; length++;
  567.                   }
  568.                  j=Get(&mem,1);
  569.                 }
  570.                mem[0]=0x00;
  571.                Out(&mem,1); offset++; length++;
  572.               }
  573.             }
  574.            else
  575.             {
  576.              Stop(7);
  577.             }
  578.           }
  579.          j=Get(&mem,1);
  580.          chr=mem[0];
  581.         }
  582. ende:
  583.        byte=0; langwort=offset;
  584.        for(i=0;i<1000;i++)
  585.         {
  586.          if(Offset[i]==0)
  587.           {
  588.            Out(&byte,1); offset++;
  589.           }
  590.          else
  591.           {
  592.            Out(&Flags[i],1);
  593.            Out(&Length[i],2);
  594.            Out(&Offset[i],4);
  595.            Out(&TOffset[i],4);
  596.            Out(&GOffset[i],4);
  597.            Out(&MOffset[i],4);
  598.            Out(&POffset[i],4);
  599.            offset+=1;
  600.            offset+=2;
  601.            offset+=4;
  602.            offset+=4;
  603.            offset+=4;
  604.            offset+=4;
  605.            offset+=4;
  606.           }
  607.         }
  608.        for(i=0;i<100;i++)
  609.         {
  610.          Out(&PNOffset[i],4);
  611.          offset+=4;
  612.         }
  613.        Out(&langwort,4); offset+=4;
  614.        printf("Check:  %lx  -  %lx\n",OPos,offset);
  615.        printf("Schreibe Hilfe-Datei ...\n");
  616.        langwort=Write(out,OutBuf,OPos);
  617.        Close(out);
  618.        if(langwort!=OPos)
  619.         {
  620.          printf("FEHLER! Übersetzte Hilfe-Datei kann nicht gespeichert werden!\n");
  621.          DeleteFile(argv[2]);
  622.         }
  623.        printf("Fertig.\n");
  624.       }
  625.      else
  626.       {
  627.        printf("Ausgabedatei \"%s\" konnte nicht eröffnet werden!\n",argv[2]);
  628.       }
  629.      FreeMem(InBuf,BufferSize);
  630.     }
  631.    else
  632.     {
  633.      printf("Nicht genug Speicher für Compiliervorgang!\n");
  634.     }
  635.    Close(in);
  636.   }
  637.  else
  638.   {
  639.    printf("Eingabedatei \"%s\" konnte nicht geöffnet werden!\n",argv[1]);
  640.   }
  641.  exit(0);
  642. }
  643.  
  644.